package util;

import java.util.Scanner;

/**
 * @Author: lya
 * @Description: 本类的作用描述:找俩个字符串中的最大长度的公串
 * @CreateDate: 2019/8/13
 * @Version: 1.0
 */
@SuppressWarnings("all")
public class StringCommonDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入人第一个字符串:>");
        String first = sc.nextLine();

        System.out.println("请输入人第二个字符串:>");
        String two = sc.nextLine();

        printCommon(first,two);

    }

    private static void printCommon(String first, String two) {
        //定义一个变量
        int len = 0;//上一个子串的长度

        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < two.length(); i++) {
            for (int j = i+1; j <= two.length(); j++) {
                //截取 - 将第二个字符串的所有的子串全部找出来
                String substr = two.substring(i,j);
                //获取子串的长度
                int sublen = substr.length();
                //判断这些子串是否在first中存在
                if(first.contains(substr) && sublen>len){
                    //清空
                    stringBuilder.delete(0,stringBuilder.capacity());
                    stringBuilder.append(substr+" ");
                    //把当前的子串的长度给len
                    len = sublen;

                }else if(first.contains(substr) && sublen == len ){
                    stringBuilder.append(substr);

                }
            }
        }
        System.out.println(stringBuilder);
    }
}
